from rest_framework import exceptions

from testviewset.models import User, Token
from utils.redisMethod import getTokenUser, setToken


class WebAuth(object):
    def authenticate(self, request):
        token = request.META.get('HTTP_TOKEN')
        user = getTokenUser(token)
        if user:
            # print(type(user))
            # for k in user.__dict__.keys():
            #     print(k, ':', getattr(user, k))
            print("redis")
            return user, token
        token = Token.objects.filter(token=token)
        # print(token.query)
        token = token.first()
        if token:
            print("sql")
            setToken(token.token, token.user)
            return token.user, token.token
        raise exceptions.APIException("请登录")

    def authenticate_header(self, request):
        pass

